package com.xjtu.conan.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjtu.conan.entity.TheatreInfo;
import com.xjtu.conan.entity.UserInfo;
import com.xjtu.conan.exception.MyException;
import com.xjtu.conan.exception.ResultEnum;
import com.xjtu.conan.mapper.TheatreInfoMapper;
import com.xjtu.conan.service.TheatreInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjtu.conan.utils.DocumentUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 剧场版表 服务实现类
 * </p>
 *
 * @author 一只黑猩猩
 * @since 2021-04-19
 */
@Slf4j
@Service
public class TheatreInfoServiceImpl extends ServiceImpl<TheatreInfoMapper, TheatreInfo> implements TheatreInfoService {

    @Autowired
    private TheatreInfoMapper theatreInfoMapper;

    @Override
    public List<TheatreInfo> getAllTheatreInfo(int current, int size) {
        Page<TheatreInfo> page = new Page<>(current, size);
        theatreInfoMapper.selectPage(page, null);
        log.info(String.valueOf(page.getTotal()));
        return page.getRecords();
    }

    @Override
    public TheatreInfo getTheatreByid(Long tid) {
        TheatreInfo theatre = theatreInfoMapper.selectById(tid);
        if (theatre == null) {
            throw new MyException(500, "不存在该剧场版！");
        }
        return theatre;
    }

    @Override
    public Long addTheatreInfo(MultipartFile video, TheatreInfo theatre) throws IOException {
        try {
            String fileName = video.getOriginalFilename();
            assert fileName != null;
            if (fileName.endsWith("mp4") || fileName.endsWith("avi")) {
                Map<String, String> temps = DocumentUtil.saveDocument(fileName);
                String savePath = temps.get("filePath");
                String saveUrl = temps.get("fileUrl");
                theatre.setTheatrePath(savePath)
                        .setTheatreUrl(saveUrl);
                video.transferTo(new File(savePath));

                if (theatreInfoMapper.insert(theatre) == 1) {
                    return theatre.getTid();
                } else
                    throw new MyException(500, "添加剧场版失败！");
            } else {
                throw new MyException(400, "文件" + fileName + "格式错误！请上传正确的图片文件！");
            }
        } catch (Exception e) {
            throw new MyException(500, "添加剧场版失败！");
        }
    }

    @Override
    public Long updateTheatreInfo(MultipartFile video, TheatreInfo theatre) {
        try {
            String fileName = video.getOriginalFilename();
            assert fileName != null;
            if (fileName.endsWith("mp4") || fileName.endsWith("avi")) {
                Map<String, String> temps = DocumentUtil.saveDocument(fileName);
                String savePath = temps.get("filePath");
                String saveUrl = temps.get("fileUrl");
                String originalFile = theatre.getTheatrePath();
                theatre.setTheatrePath(savePath)
                        .setTheatreUrl(saveUrl);
                video.transferTo(new File(savePath));

                if (theatreInfoMapper.updateById(theatre) == 1) {
                    File file = new File(originalFile);
                    if (file.exists()) {
                        boolean delete = file.delete();
                        log.info("删除原剧场版文件成功！");
                    }
                    return theatre.getTid();
                } else
                    throw new MyException(500, "更新剧场版失败！");
            } else {
                throw new MyException(400, "文件" + fileName + "格式错误！请上传正确的图片文件！");
            }
        } catch (Exception e) {
            throw new MyException(500, "更新剧场版失败！");
        }
    }

    @Override
    public int deleteTheatreInfo(Long tid) {
        try {
            String originalFile = this.getTheatreByid(tid).getTheatrePath();
            if (theatreInfoMapper.deleteById(tid) == 1) {
                File file = new File(originalFile);
                if (file.exists()) {
                    boolean delete = file.delete();
                    log.info("删除原剧场版文件成功！");
                }
            }
            return 1;
        } catch (Exception e) {
            throw new MyException(500, "删除剧场版失败！");
        }

    }

    @Override
    public int getTotal() {
        return theatreInfoMapper.selectCount(null);
    }
}
